package com.qianfeng.openapi.web.master.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qianfeng.openapi.web.master.dto.TableData;
import com.qianfeng.openapi.web.master.dto.TableParam;
import com.qianfeng.openapi.web.master.entity.ScheduleJob;
import com.qianfeng.openapi.web.master.mapper.ScheduleJobMapper;
import com.qianfeng.openapi.web.master.service.ScheduleJobService;
import com.qianfeng.openapi.web.master.utils.QuartzUtils;
import com.qianfeng.openapi.web.master.utils.SysConstant;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.Scheduler;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description:
 * @Company: 千锋互联
 * @Author: 李丽婷
 * @Date: 2020/11/27
 * @Time: 下午3:01
 */
@Service
public class ScheduleJobServiceImpl implements ScheduleJobService {

    @Resource
    private ScheduleJobMapper scheduleJobMapper;
    @Resource
    private Scheduler scheduler;

    Log logger = LogFactory.getLog(ScheduleJobServiceImpl.class);

    @Override
    public R saveSchedule(ScheduleJob scheduleJob) {
        //mybatis-plus
        //1.读Schdule_job表进行操作
        int i = scheduleJobMapper.insert(scheduleJob);
        if(i>0){
            //2.创建真正的定时任务
            QuartzUtils.createTask(scheduleJob,scheduler);
        }

        return i>0?R.ok(null):R.failed("创建定时任务失败");
    }

    @Override
    public R updateSchedule(ScheduleJob scheduleJob) {
        logger.info("---->修改定时任务"+ scheduleJob.toString());

        //mybatis-plus
        //1.读Schdule_job表进行操作
        int i = scheduleJobMapper.updateById(scheduleJob);
        if(i>0) {
            //2.修改真正的定时任务
            QuartzUtils.updateScheduler(scheduleJob, scheduler);
        }
        return i>0?R.ok(null):R.failed("修改定时任务失败");
    }

    @Override
    public R deleteSchedule(List<Long> ids) {
        logger.info("---->删除定时任务"+ ids.toString());

        int i = scheduleJobMapper.deleteBatchIds(ids);
        //2.删除真正的定时任务
        if(ids!=null&&ids.size()>0) {
            for (Long id : ids) {
                QuartzUtils.deleteScheduler(id, scheduler);
            }

        }
        return i>0?R.ok(null):R.failed("删除定时任务失败");
    }

    /**
     * 暂停  status=1
     * @param ids
     * @return
     */
    @Override
    public R pauseSchedule(List<Long> ids) {
        logger.info("---->暂停定时任务"+ ids.toString());

        int i = 0;
        for (Long id : ids) {
            ScheduleJob job = new ScheduleJob();
            job.setJobId(id);
            job.setStatus(SysConstant.PAUSE);
             i = scheduleJobMapper.updateById(job);
             if (i>0){
                 QuartzUtils.pauseScheduler(id,scheduler);
             }
        }

        return i>0?R.ok(null):R.failed("暂停定时任务失败");    }

    @Override
    public R resumeSchedule(List<Long> ids) {
        logger.info("---->恢复定时任务"+ ids.toString());
        int i = 0;
        for (Long id : ids) {
            ScheduleJob job = new ScheduleJob();
            job.setJobId(id);
            job.setStatus(SysConstant.NOMAL);
            i = scheduleJobMapper.updateById(job);

            if (i>0){
                QuartzUtils.resumeScheduler(id,scheduler);
            }
        }

        return i>0?R.ok(null):R.failed("暂停定时任务失败");
    }

    @Override
    public R runOnceSchedule(List<Long> ids) {

        for (Long id : ids) {
            QuartzUtils.runOnceTask(id,scheduler);
        }

        return  R.ok(null);
    }

    @Override
    public R findScheduleById(Long id) {
        ScheduleJob scheduleJob = scheduleJobMapper.selectById(id);

        return R.ok(scheduleJob);
    }

    @Override
    public TableData scheduleList(TableParam param) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (param.getSearch()!=null&&!param.getSearch().equals("")){
            queryWrapper.like("bean_name",param.getSearch());
            queryWrapper.or();
            queryWrapper.like("method_name",param.getSearch());
        }
        IPage page = new Page();
        page.setCurrent(param.getOffset()/param.getLimit()+1);
        page.setSize(param.getLimit());

        page =  scheduleJobMapper.selectPage(page,queryWrapper);

        TableData data = new TableData();
        data.setTotal(page.getTotal());
        data.setRows(page.getRecords());
        return data;
    }
}
